package com.maxwell.googleplay.protocol;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
import com.maxwell.googleplay.base.BaseProtocol;
import com.maxwell.googleplay.bean.AppInfoBean;

/**
 * @author  yue
 * @创建时间 2016-4-1上午11:10:57
 * @描述     TODO
 * 
 */
public class GameProtocol extends BaseProtocol<List<AppInfoBean>> {

	@Override
	public String getInterfaceKey() {
		// TODO Auto-generated method stub
		return "game";
	}

	@Override
	public List<AppInfoBean> parseJson(String jsonString) {
		Gson gson = new Gson();
		/*===========泛型解析================*/
		//return gson.fromJson(jsonString, new TypeToken<List<AppInfoBean>>(){}.getType());
		
		
		/*===========Gson节点解析================*/
		List<AppInfoBean> appInfoBeans = new ArrayList<AppInfoBean>();
		
		//获得解析器
		JsonParser parser = new JsonParser();
		JsonElement rootJsonElement = parser.parse(jsonString);
		
		//rootJsonElement ----->jsonArray
		JsonArray rootJsonArray = rootJsonElement.getAsJsonArray();
		
		//遍历rootJsonArray
		for(JsonElement itemJsonElement: rootJsonArray){
			//itemJsonElement--->转化成JsonObject
			JsonObject itemJsonObject = itemJsonElement.getAsJsonObject();
			
			//得到具体的JsonPrimitive
			JsonPrimitive desJsonPrimitive = itemJsonObject.getAsJsonPrimitive("des");
			//JsonPrimitive---->具体的类型
			String des = desJsonPrimitive.getAsString();
			
			//得到具体的JsonPrimitive
			JsonPrimitive downloadUrlPrimitive = itemJsonObject.getAsJsonPrimitive("downloadUrl");
			//JsonPrimitive---->具体的类型
			String downloadUrl = desJsonPrimitive.getAsString();
			
			
			String iconUrl = itemJsonObject.get("iconUrl").getAsString();
			Long id = itemJsonObject.get("id").getAsLong();
			String name = itemJsonObject.get("name").getAsString();
			String packageName = itemJsonObject.get("packageName").getAsString();
			Long size = itemJsonObject.get("size").getAsLong();
			Float stars = itemJsonObject.get("stars").getAsFloat();
			
			
			AppInfoBean info = new AppInfoBean();
			info.des = des;
			info.id = id;
			info.downloadUrl = downloadUrl;
			info.name = name;
			info.packageName = packageName;
			info.stars = stars;
			info.size = size;
			info.iconUrl = iconUrl;
			
			//添加到集合中
			appInfoBeans.add(info);
		}
		
		
		//返回结果
		return appInfoBeans;
		
		
	}

}
